#课堂练习
import pygame,sys,random
from pygame.locals import *

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self,image_file,speed,location):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(image_file)
        self.rect=self.image.get_rect()
        self.rect.left,self.rect.top=location
        self.speed=speed

    def move(self):
        global points,score_text
        self.rect=self.rect.move(self.speed)
        if self.rect.left<0 or self.rect.right>screen.get_width():
            self.speed[0]=-self.speed[0]

        if self.rect.top<=0:
            self.speed[1]=-self.speed[1]
            points=points+1
            score_text=font.render(str(points),1,(0,0,0))

        if self.rect.top>=screen.get_rect().bottom:
            global lives
            lives=lives-1
            if lives==0:
                final_text1="Game Over"
                final_text2="Your final score is:"+str(points)
                ft1_font=pygame.font.Font(None,70)
                ft1_surf=font.render(final_text1,1,(0,0,0))
                ft2_font=pygame.font.Font(None,50)
                ft2_surf=font.render(final_text2,1,(0,0,0))
                screen.blit(ft1_surf,[screen.get_width()/2-ft1_surf.get_width()/2,100])
                screen.blit(ft2_surf,[screen.get_width()/2-ft2_surf.get_width()/2,200])
                pygame.display.flip()
                global done
                done=True
            else:
                pygame.time.delay(2000)
                self.rect.topleft=[(screen.get_rect().width)-40*lives,20]


class MyPaddleClass(pygame.sprite.Sprite):
    def __init__(self,location=[0,0]):
        pygame.sprite.Sprite.__init__(self)
        image_surface=pygame.surface.Surface([100,20])
        image_surface.fill([0,0,0])
        self.image=image_surface.convert()
        self.rect=self.image.get_rect()
        self.rect.left,self.rect.top=location

pygame.init()
size=width,height=640,480
screen=pygame.display.set_mode(size)
clock=pygame.time.Clock()
ball_speed=[random.randint(3,10),random.randint(2,6)]
myBall=MyBallClass('Image\\wackyball.bmp',ball_speed,[random.randint(12,51),random.randint(6,51)])
ballGroup=pygame.sprite.Group(myBall)
paddle=MyPaddleClass([270,400])

font=pygame.font.Font(None,50)
points=0
score_text=font.render(str(points),1,(0,0,0))
textpos=[10,10]

lives=3
done=False

while True:
    clock.tick(60) 
    screen.fill([255,255,255])
    screen.blit(score_text,textpos)
    for i in range(lives):
        width=screen.get_rect().width
        screen.blit(myBall.image,[width-40*i,20])
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()

        if event.type==pygame.MOUSEMOTION:
            paddle.rect.centerx=event.pos[0]

    if pygame.sprite.spritecollide(paddle,ballGroup,False):
        myBall.speed[1]=-myBall.speed[1]
    myBall.move()
    if not done:
        screen.blit(myBall.image,myBall.rect)
        screen.blit(paddle.image,paddle.rect)
        screen.blit(score_text,textpos)
        for i in range(lives):
            widht=screen.get_width()
            screen.blit(myBall.image,[width-40*i,20])
        pygame.display.flip()
    

    pygame.display.flip()